package com.i2bt.app.set;

import com.i2bt.common.AndroidRestContract;

import com.i2bt.R;
import com.i2bt.app.BaseActivity;
import com.i2bt.app.set.model.BindJsonHandle;
import com.i2bt.bean.BindHomeBoxBean;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class BindHomeBoxActivity extends BaseActivity implements TextWatcher {
	private Button bindButton;
	private EditText userName, password, homeboxName;
	private TextView help, identifyCode, warnToast;
	private String userNameStr, passwordStr, homeboxNameStr;

	@Override
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_set_bind_homebox);
		initView();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		boolean result = super.onCreateOptionsMenu(menu);
		this.setActionMenuGroupVisible(false);
		this.setActionBarBack("设置");
		return result;
	}

	/**
	 * 点击空白处隐藏输入法
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		InputMethodManager manager;
		manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (getCurrentFocus() != null
					&& getCurrentFocus().getWindowToken() != null) {
				manager.hideSoftInputFromWindow(getCurrentFocus()
						.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
		return super.onTouchEvent(event);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (arg0 == bindButton) {
			warnToast.setVisibility(View.INVISIBLE);
			userNameStr = userName.getText().toString();
			passwordStr = password.getText().toString();
			try {
				homeboxNameStr = URLEncoder.encode(homeboxName.getText()
						.toString(), "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			logic();
		}
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		bindButton = (Button) this.findViewById(R.id.set_bind_homebox_button);
		userName = (EditText) this.findViewById(R.id.set_bind_homebox_UserName);
		password = (EditText) this.findViewById(R.id.set_bind_homebox_password);
		homeboxName = (EditText) this
				.findViewById(R.id.set_bind_homebox_homeboxname);
		help = (TextView) this.findViewById(R.id.set_bind_homebox_help);
		identifyCode = (TextView) this
				.findViewById(R.id.set_bind_homebox_identifyCode);
		warnToast = (TextView) this.findViewById(R.id.set_bind_homebox_warn);
		bindButton.setOnClickListener(this);
		password.addTextChangedListener(this);
		userName.addTextChangedListener(this);
		this.setIdentifyCode("");
		this.help.setText("		提示： 输入私密云账号和密码获取验证码,随后请将验证码输入到Homebox中完成绑定!");
	}

	@Override
	protected void logic() {
		// TODO Auto-generated method stub
		if (userNameStr.length() == 0) {
			showWarnToast("用户名不能为空");
		} else if (passwordStr.length() == 0) {
			showWarnToast("密码不能为空");
		} else if (passwordStr.length() < 6) {
			showWarnToast("密码长度不对,最小需要六位");
		} else if (homeboxNameStr.length() == 0) {
			showWarnToast("请给HomeBox取名!");
		} else {

			BindHomeBoxBean bean = new BindHomeBoxBean();
			bean.setAct(AndroidRestContract.BIND_HOMEBOX);
			bean.setUserId(userNameStr);
			bean.setPassword(passwordStr);
			bean.setHomeboxName(homeboxNameStr);
			new BindJsonHandle(this).postTo(
					AndroidRestContract.REST_BIND_HOMEBOX, bean);
		}
	}

	/**
	 * 显示警告信息
	 * 
	 * @param Str显示的信息
	 */
	public void showWarnToast(String Str) {
		warnToast.setText(Str);
		warnToast.setVisibility(View.VISIBLE);
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub
		if (arg0.length() > 0) {
			int pos = arg0.length() - 1;
			char c = arg0.charAt(pos);
			if (c == ' ' || c == '.') {
				arg0.delete(pos, pos + 1);
				toastHandler("输入违法!");
			}
		}
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		if (warnToast.getVisibility() == View.VISIBLE) {
			if ((!password.getText().toString().equals(""))
					&& (!userName.getText().toString().equals(""))) {
				warnToast.setVisibility(View.INVISIBLE);
			}
		}
	}

	public String getUserNameStr() {
		return userNameStr;
	}

	public void setIdentifyCode(String str) {
		this.identifyCode.setText(str);
	}

	public String getPasswordStr() {
		return passwordStr;
	}

	@Override
	public void backHomeOperate() {
		// TODO Auto-generated method stub
		finish();
	}
}
